package tools

import (
	"fmt"
	"strconv"
	"strings"
)

// 转换字节大小
func ParseSize(sizeStr string) (int64, error) {
	sizeStr = strings.TrimSpace(sizeStr)
	unit := sizeStr[len(sizeStr)-1:]
	size := sizeStr[:len(sizeStr)-1]

	bytes, err := strconv.ParseInt(size, 10, 64)
	if err != nil {
		return 0, err
	}

	switch strings.ToUpper(unit) {
	case "B":
		return bytes, nil
	case "K":
		return bytes * 1024, nil
	case "M":
		return bytes * 1024 * 1024, nil
	case "G":
		return bytes * 1024 * 1024 * 1024, nil
	case "T":
		return bytes * 1024 * 1024 * 1024 * 1024, nil
	default:
		return 0, fmt.Errorf("unsupported unit: %s", unit)
	}
}
